<?php
    /*
     * 参数防火墙
     * 如果发现特殊字符，则直接拒绝执行，不再尝试过滤
     *
    **/
    class Fireware{
        /**
         * 
         * 参数过滤方法
         * 
         */
        public function run (&$params){
            $this->securityFilter();
            $this->filterGetPost();
        }

        /**
         * 检查参数中是否含有特殊字符
         *
         * 该方法使用了递归
         *
         * @param string|array $param
         *            参数数组
         */
        public static function paramsFilter ($param){
            if (is_array($param)) {
                foreach ($param as $k => $v) {
                    self::paramsFilter($k);
                    self::paramsFilter($v);
                }
            } elseif ($param != htmlspecialchars((string) $param, ENT_QUOTES)) {
                die('Params, Illegal argument, Access Denied!');
            }
            return true;
        }

        /*
         * 过滤sql关键字
         * $param url参数
         */
        protected function dowith_sql($str){
            $str = str_replace("_", "", $str);
            $str = str_replace("%", "", $str);
            $str = str_replace(";", "", $str);
            $str = str_replace("\'", "", $str);
            $str = str_replace("'", "", $str);
            $str = str_replace('"', "", $str);
            $str = str_replace(" ", "", $str);
            $str = str_replace("delete", "", $str);
            $str = str_replace("and", "", $str);
            $str = str_replace("or", "", $str);
            $str = str_replace("execute", "", $str);
            $str = str_replace("insert", "", $str);
            $str = str_replace("select", "", $str);
            $str = str_replace("update", "", $str);
            $str = str_replace("alter", "", $str);
            $str = str_replace("create", "", $str);
            $str = str_replace("drop", "", $str);
            $str = str_replace("count", "", $str);
            $str = str_replace("asc", "", $str);
            $str = str_replace("substring", "", $str);
            $str = str_replace("master", "", $str);
            $str = str_replace("truncate", "", $str);
            $str = str_replace("=", "",$str);
            $str = str_replace("%20", "", $str);
            return $str;
        }

        /**
         * 参数安全过滤
         *
         * 对GET、POST 进行了校验
         *
         */
        protected function filterGetPost (){
            // 全局检查GET参数，遇到非法字符直接报错，不在往下执行
            if (isset($_GET) && count($_GET) > 0) {
                $_GET = self::filter_get_post($_GET);
                self::paramsFilter($_GET);
            }
            // 检查POST参数是否含有特殊字符，遇到非法字符直接报错，不在往下执行
            if (isset($_POST) && count($_POST) > 0) {
                $_POST = self::filter_get_post($_POST);
                self::paramsFilter($_POST);
            }
        }

        /**
         * 参数xss过滤
         *
         * 对GET、POST 数据进行过滤
         *
         */
        protected function filter_get_post ($record){
            foreach ($record as $k => $v) {
                $record[$k] = preg_replace("![\][xX]([A-Fa-f0-9]{1,3})!","",remove_xss($v));
                $record[$k] = $this->dowith_sql($record[$k]);
            }
            return $record;
        }

        /**
         * 参数安全过滤
         *
         * 对REFERER、PHP_SELF 进行了校验
         *
         */
        public function securityFilter (){
            /**
             * Referer参数过滤
             *
             * @since 2012-03-30
             */
            if (isset($_SERVER['HTTP_REFERER']) &&
                strlen($_SERVER['HTTP_REFERER']) > 0) {
                $tmp_search = array('<', '>', '%3C', '%3E', "\'", '"', ';');
                $tmp_replace = array('', '', '',  '',  '', '', '');
                $tmp_reffer = str_replace($tmp_search, $tmp_replace, $_SERVER['HTTP_REFERER']);
                if ($tmp_reffer != $_SERVER['HTTP_REFERER']) {
                    die('HTTP_REFERER, Illegal argument, Access Denied!');
                }
                unset($tmp_search);
                unset($tmp_replace);
                unset($tmp_reffer);
            }
            /**
             * 对$_SERVER['PHP_SELF']进行判断是否含特殊字符
             */
            if (isset($_SERVER['PHP_SELF']) && htmlspecialchars(
                    $_SERVER['PHP_SELF'], ENT_QUOTES) != $_SERVER['PHP_SELF']) {
                die('PHP_SELF, Illegal argument, Access Denied!!');
            }
        }
    }
?>